﻿using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimSdkTestDemo
{
    public class HikWaveProvider : IWaveProvider
    {
        private byte[] _buffer = new byte[1024 * 200];
        private int _readOffset = 0;
        private int _writeOffset = 0;
        private Queue<byte> waveQueue = new Queue<byte>();
        public WaveFormat WaveFormat { get; set; }
        public HikWaveProvider(WaveFormat waveFormat)
        {
            WaveFormat = waveFormat;
        }
        public int Read(byte[] buffer, int offset, int count)
        {
            if (count + _readOffset < _buffer.Length)
            {
                Array.Copy(_buffer, _readOffset, buffer, 0, count);
                _readOffset += count;
            }
            else
            {
                var lastCount = _buffer.Length - _readOffset;
                Array.Copy(_buffer, _readOffset, buffer, 0, lastCount);
                Array.Copy(_buffer, 0, buffer, lastCount, count - lastCount);
                _readOffset = count - lastCount;
            }
            return 14400;
        }

        public void Write(byte[] buffer)
        {
            if (_writeOffset + buffer.Length > _buffer.Length)
            {
                var p = _buffer.Length - _writeOffset;
                if (p > 0)
                {
                    Array.Copy(buffer, 0, _buffer, _writeOffset, p);
                }
                Array.Copy(buffer, p, _buffer, 0, buffer.Length - p);
                _writeOffset = buffer.Length - p;
            }
            else
            {
                Array.Copy(buffer, 0, _buffer, _writeOffset, buffer.Length);
            }
            _writeOffset += buffer.Length;
        }
    }
}
